package com.zkq.graduate.service.impl;

import com.zkq.graduate.mapper.FileRecordMapper;
import com.zkq.graduate.mapper.SelectedTitleMapper;
import com.zkq.graduate.pojo.StudentVo;
import com.zkq.graduate.pojo.StudentWithReport;
import com.zkq.graduate.pojo.Title;
import com.zkq.graduate.service.FileRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author zhangkaiqiang
 * @Date 2019/12/14  22:10
 * @Description TODO
 */
@Service("fileRecordService")
public class FileRecordServiceImpl implements FileRecordService {


	@Resource
	private FileRecordMapper fileRecordMapper;

	@Resource
	private SelectedTitleMapper selectedTitleMapper;

	@Override
	public boolean insertOneRecord(StudentWithReport studentWithReport) {
		String studentId=studentWithReport.getStudentId();
		List<StudentVo> selectList=selectedTitleMapper.findStudentSelectedTitle(studentId);
		StudentVo studentVo=new StudentVo();
		for(StudentVo st:selectList){
			if(Objects.equals("达成双选",st.getSelectStatus())){
				studentVo=st;
			}
		}
		Title title=Title.builder().id(studentVo.getTitle().getId()).build();
		studentWithReport.setTitle(title);
		return fileRecordMapper.insertOneRecord(studentWithReport)==1;
	}

	@Override
	public List<StudentWithReport> findByStudentId(String studentId) {
		List<StudentWithReport> list=new ArrayList<>();
		if(StringUtils.isNotBlank(studentId)){
			list=fileRecordMapper.findByStudentId(studentId);
		}
		if(list.size()>0){
			list.get(0).setLast(true);
			return list;
		}
		return new ArrayList<>();
	}

	@Override
	public List<StudentWithReport> findRecordByteacherId(@NotNull String teacherId) {
		List<StudentWithReport> student=fileRecordMapper.findRecordByteacherId(teacherId);
		if(student.size()>0){
			return student;
		}
		return new ArrayList<>();
	}

	@Override
	public int deleteByStudentId(String studentId) {
		return fileRecordMapper.deleteByStudentId(studentId);
	}

	@Override
	public int updateByStudentId(StudentWithReport student) {
		return fileRecordMapper.updateByStudentId(student);
	}

	@Override
	public boolean updateLast(StudentWithReport student) {
		student.setLast(true);
		student.setStatus("1");
		return fileRecordMapper.updateLast(student)==1;
	}
}
